import {store} from "@/js/store";

let soulMagicMarkType=store.state.soulMagicMarkType
export function mpPlus(mp1, mp2) {
    let text1 = /\(.*\)/.exec(mp1)
    if (typeof mp2 !== 'function') {
        let num = parseInt(mp1) + parseInt(mp2)
        let text2 = /\(.*\)/.exec(mp2)
        if (text1 != null && text2 != null) {
            return num + text1[0].substring(0, text1[0].length - 1) + text2[0].replace('(', ',')
        } else if (text1 !== null) {
            return num + text1[0]
        } else if (text2 !== null) {
            return num + text2[0]
        } else {
            return num
        }
    } else {
        let num = parseInt(mp1)
        num = mp2(num)
        return mpPlus(0 + text1, num)
    }

}

export function totalComplexity(mark) {
    let subCom = 0;
    let changeCom = 0;
    for (const sub of mark.subMark) {
        subCom += totalComplexity(sub);
    }
    for (const key of Object.keys(mark.changeAble)) {
        if (mark.changeAble[key].length === 0) {
            changeCom++
        }
    }
    return parseInt(mark.complexity) + subCom + changeCom
}

export function allChangeAble(mark) {
    let a = []
    for (const key of Object.keys(mark.changeAble)) {
        if (mark.changeAble[key].length === 0) {
            a.push(mark.name + '-Δ' + key)
        } else {
            a.push(mark.name + '-' + key + ':' + mark.changeAble[key])
        }
    }
    for (const sub of mark.subMark) {
        a = [...a, ...allChangeAble(sub)]
    }
    return a
}

export function checkMark(mark) {
    let slots = mark.as(mark)
    let need =mark.subMark.slice()
    let slotsLength = slots.length
    let unusedSlots=[]
    for (let i = 0; i < slotsLength; i++) {
        let needLength = need.length
        for (let j = 0; j < needLength; j++) {
            if ((slots[i] === need[j].need) || (slots[i] === '任何') || (need[j].need === '任何')) {
                need.splice(j, 1)
                break
            }
        }
        unusedSlots.push(slots[i])

    }
    return [need,unusedSlots];

}


export function getMp(mark) {
    if (mark.subMark === []) {
        if (typeof mark.mp !== "function") {
            return mark.mp
        } else {
            return mark.mp(0)
        }
    } else {
        let subMp = 0;
        for (const sub of mark.subMark) {
            subMp = mpPlus(subMp, getMp(sub))
        }

        if (typeof mark.mp !== "function") {
            return mpPlus(mark.mp, subMp)
        } else {
            return mark.mp(subMp)
        }
    }
}

export const SoulMark = (
    name = '',
    subMark = [],
    complexity,
    mp,
    changeAble,
    as,
    need,
    check
) => {
    let out
    if (name === '') {
        out = {
            name: soulMagicMarkType['magicStart'].name,
            complexity: soulMagicMarkType['magicStart'].complexity,
            mp: soulMagicMarkType['magicStart'].mp,
            changeAble: soulMagicMarkType['magicStart'].changeAble,
            as: soulMagicMarkType['magicStart'].as,
            need: soulMagicMarkType['magicStart'].need,
            check: soulMagicMarkType['magicStart'].check,
            markType:'magicStart'
        }
    } else if (soulMagicMarkType[name] != null) {
        out = {
            name: soulMagicMarkType[name].name,
            complexity: soulMagicMarkType[name].complexity,
            mp: soulMagicMarkType[name].mp,
            changeAble: soulMagicMarkType[name].changeAble,
            as: soulMagicMarkType[name].as,
            need: soulMagicMarkType[name].need,
            check: soulMagicMarkType[name].check,
            markType:name
        }
    } else {
        out = {
            name: soulMagicMarkType['empty'].name,
            complexity: soulMagicMarkType['empty'].complexity,
            mp: soulMagicMarkType['empty'].mp,
            changeAble: soulMagicMarkType['empty'].changeAble,
            as: soulMagicMarkType['empty'].as,
            need: soulMagicMarkType['empty'].need,
            check: soulMagicMarkType['empty'].check,
            markType:name
        }
    }
    out.subMark = subMark
    if (complexity != null) out.complexity = complexity
    if (mp != null) out.mp = mp
    if (changeAble != null) out.changeAble = changeAble
    if (as != null) out.as = as
    if (need != null) out.need = need
    if (check != null) out.check = check
    return out
}
export const SoulMagic = (
    startMark = SoulMark(),
    name = '',
    ap = '',
    mp = '',
    effect = ''
) => {
    return {
        startMark: startMark,
        name: name,
        ap: ap,
        mp: mp,
        effect: effect
    }
}
